package cn.tedu.boot08_1.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@RestController
public class UploadController {
    @RequestMapping("/upload")
    public String upload(MultipartFile picFile) throws IOException {
        String fileName = picFile.getOriginalFilename();
        System.out.println(fileName);
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        fileName = UUID.randomUUID() + suffix;
        System.out.println(fileName);
        String dirPath = "D:/javaFiles";
        SimpleDateFormat f = new SimpleDateFormat("/yyyy/MM/dd/");
        String datePath = f.format(new Date());
        File dirFile = new File(dirPath + datePath);
        if(!dirFile.exists()) {
            dirFile.mkdirs();
        }
        picFile.transferTo(new File(dirPath + datePath + fileName));
        return datePath + fileName;
    }
    @RequestMapping("/remove")
    public void remove(String url) {
        File f = new File("D:/javaFiles" + url);
        f.delete();
    }
}
